package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.service.SpecificationService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/specification")
public class SpecificationController {

    //注入
    @Reference(timeout=10000)
    private SpecificationService specificationService;

    /** 多条件分页查询规格 */
    @GetMapping("/findByPage")
    public PageResult findByPage(Specification specification,Integer page,Integer rows){

        if(specification!=null && StringUtils.isNotBlank(specification.getSpecName())){
            try {
                specification.setSpecName(new String(
                        specification.getSpecName().getBytes("ISO8859-1"),"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            }
            return specificationService.findByPage(specification,page,rows);


    }
    //@RequestBody作用是将类对象绑定到实体类
    /** 保存添加的数据*/
    @PostMapping("/save")
    public boolean save(@RequestBody Specification specification){

        try {
            specificationService.save(specification);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /** 根据规格主键查询规格选项 */
    @GetMapping("/findSpecOption")
    public List<SpecificationOption> findSpecOption(Long id){
        return specificationService.findSpecOption(id);
    }


    /** 保存修改的数据*/
    @PostMapping("/update")
    public boolean update(@RequestBody Specification specification){

        try {
            specificationService.update(specification);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    /** 批量删除*/
    @GetMapping("/delete")
    public boolean delete(Long[]ids){

        try {
            specificationService.deleteAll(ids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     *
     * 查询所有的规格
     */
    @GetMapping("/findSpecList")
    public List<Map<String,Object>> findSpecList(){

        return specificationService.findAllByIdAndName();
    }





}
